package com.team.sion.service.Impl;

import com.team.sion.mapper.CollectMapper;
import com.team.sion.pojo.Collect;
import com.team.sion.pojo.CollectExample;
import com.team.sion.service.AdminService;
import com.team.sion.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public List<Collect> getByUserId(Integer userId) {
        CollectExample example = new CollectExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return collectMapper.selectByExample(example);
    }

    @Override
    public void addCollect(Collect collection) {
        collection.setCreatetime(new Date());
        collectMapper.insertSelective(collection);
    }

    @Override
    public void deleteCollect(Integer userId, Integer songId) {
        CollectExample example = new CollectExample();
        example.createCriteria().andUserIdEqualTo(userId).andSongIdEqualTo(songId);
        collectMapper.deleteByExample(example);
    }

    @Override
    public Boolean getStatus(Collect collect) {
        CollectExample example = new CollectExample();
        if("0".equals(collect.getType())){
            example.createCriteria().andUserIdEqualTo(collect.getUserId()).andSongIdEqualTo(collect.getSongId()).andTypeEqualTo(collect.getType());
        }else{
            //因为接受的时候直接用songId接受了 所以仅根据type比较在数据库中
            example.createCriteria().andUserIdEqualTo(collect.getUserId()).andSonglistIdEqualTo(collect.getSongId()).andTypeEqualTo(collect.getType());
        }
        List<Collect> list = collectMapper.selectByExample(example);
        if(list.size() == 0){
            return false;
        }
        return true;
    }

    @Override
    public void deleteListCollect(Integer userId, Integer songlistId) {
        CollectExample example = new CollectExample();
        example.createCriteria().andUserIdEqualTo(userId).andSonglistIdEqualTo(songlistId);
        collectMapper.deleteByExample(example);
    }

    @Override
    public Boolean deleteList(Integer userId, Integer songListId) {
        CollectExample example = new CollectExample();
        example.createCriteria().andUserIdEqualTo(userId).andSonglistIdEqualTo(songListId);
        collectMapper.deleteByExample(example);
        return true;
    }
}
